<?php
/**
 * 关注接口
 * 关注列表查询，关注用户添加
 * 验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;


use Common\Common\Tools;
use Common\Controller\ApiController;
use Think\Exception;

class FollowController extends ApiController
{
    public function rest()
    {
        switch ($this->_method) {
            case 'get': // get请求处理代码
                $this->_get();
                break;
            case 'post': // post请求处理代码
                switch (I('post.type')) {
                    case 'category':
                        $this->_addCategory();
                        break;
                    default:
                        $this->_post();
                        break;
                }
                break;
            case 'delete': // post请求处理代码
                switch (I('get.type')) {
                    case 'category':
                        $this->_delCategory();
                        break;
                    default:
                        $this->_del();
                        break;
                }
                break;
        }
    }

    private function _get()
    {
        $info = I('get.');
        $result = D('MemberFans')->get_attention_list($info['uid'], $info['type']);
        if (count($result) > 0) {
            $this->myApiPrint(C('STATUS')['200'], 200, $result);
        } else {
            $this->myApiPrint(C('STATUS')['200'], 201, []);
        }

    }

    /**
     * 添加关注
     */
    private function _post()
    {
        $info = I('post.');
        $this->addAttention($info);
    }

    /**
     * 添加关注
     * @param $info
     */
    public function addAttention($info)
    {
        if ($info['attention_uid'] == $info['fans_uid']) {
            $this->myApiPrint(C('STATUS')['211'], 400);
        }
        if (D('MemberFans')->is_follow($info['attention_uid'], $info['fans_uid']) > 0) {
            $this->myApiPrint(C('STATUS')['217'], 401);
        }
        //粉丝设置
        $data['attention_uid'] = $info['attention_uid'];
        $data['fans_uid'] = $info['fans_uid'];
        $data['addtime'] = time();
        $data['from_truename'] = !empty($info['from_truename']) ? $info['from_truename'] : '';
        $result = D('MemberFans')->add($data);
        if ($result) {
            $this->addMessageAttention($info['fans_uid'], $info['attention_uid']);  //关注消息设置
            $this->myApiPrint(C('STATUS')['200'], 200);
        }
    }

    /**
     * 取消关注
     */
    public function _del()
    {
        $uid = I('request.uid');
        $fans_uid = !empty(I('request.fans_uid')) ? I('request.fans_uid') : $this->userId;
        try {
            $result = D('MemberFans')->del_attention($uid, $fans_uid);
            if ($result) {
                $this->myApiPrint(C('STATUS')['200'], 200, $result);
            } else {
                $this->myApiPrint(C('STATUS')['219'], 201, false);
            }
        } catch (Exception $exception) {
            $this->myApiPrint(C('STATUS')['101'], 400, false);
        }
    }

    /**
     * 关注消息设置
     * @param $from_uid
     * @param $to_uid
     */
    public function addMessageAttention($from_uid, $to_uid)
    {
        $data['from_uid'] = $from_uid;
        $data['to_uid'] = $to_uid;
        $data['addtime'] = time();
        $result = D('MessageAttention')->add($data);
        if ($result) {
            $name = D('MemberProfile')->get_uid_by_name($from_uid);
            $content = $name . C('MESS_TYPE')['attention'];
            Tools::Jpush_Send($to_uid, $content);//极光推送（关注消息）
        }
    }

    /**
     * 关注农作物
     */
    private function _addCategory()
    {
        $cat_id = I('post.cat_id');
        $u_id = $this->userId;
        $result = D('SelectedCategory')->addSelectCategory($cat_id, $u_id);
        $this->myApiPrint($result['msg'], $result['code'], $result['result']);
    }

    /**
     * 取消关注农作物
     */
    private function _delCategory()
    {
        $cat_id = I('get.cat_id');
        $u_id = $this->userId;
        $result = D('SelectedCategory')->del_category($cat_id, $u_id);
        $this->myApiPrint($result['msg'], $result['code'], $result['result']);
    }
}
